<template>
  <div class="login-log-container page-container">
    <div class="page-header">
      <h2>登录日志</h2>
    </div>
    
    <!-- 搜索区域 -->
    <div class="search-container">
      <el-form :inline="true" :model="queryParams" class="search-form">
        <el-form-item label="用户ID">
          <el-input v-model="queryParams.userId" placeholder="请输入用户ID" clearable />
        </el-form-item>
        <el-form-item label="开始时间">
          <el-date-picker
            v-model="queryParams.startTime"
            type="datetime"
            placeholder="选择开始时间"
            format="YYYY-MM-DD HH:mm:ss"
            value-format="YYYY-MM-DD HH:mm:ss"
            clearable
            style="width: 220px;"
          />
        </el-form-item>
        <el-form-item label="结束时间">
          <el-date-picker
            v-model="queryParams.endTime"
            type="datetime"
            placeholder="选择结束时间"
            format="YYYY-MM-DD HH:mm:ss"
            value-format="YYYY-MM-DD HH:mm:ss"
            clearable
            style="width: 220px;"
          />
        </el-form-item>
        <el-form-item>
          <div class="button-group">
            <el-button type="primary" @click="handleQuery">
              <el-icon><Search /></el-icon> 搜索
            </el-button>
            <el-button @click="resetQuery">
              <el-icon><Refresh /></el-icon> 重置
            </el-button>
          </div>
        </el-form-item>
      </el-form>
    </div>
    
    <!-- 日志列表 -->
    <div class="table-card">
      <el-table
        v-loading="loading"
        :data="logList"
        border
        style="width: 100%"
        :stripe="true"
      >
        <el-table-column prop="id" label="日志ID" width="80" align="center" />
        <el-table-column prop="userId" label="用户ID" width="100" align="center" />
        <el-table-column prop="loginTime" label="登录时间" width="180" align="center" />
        <el-table-column prop="ipAddress" label="登录IP" width="150" align="center" />
        <el-table-column label="状态" width="100" align="center">
          <template #default="scope">
            <el-tag :type="scope.row.status === 'success' ? 'success' : 'danger'">
              {{ scope.row.status === 'success' ? '成功' : '失败' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="message" label="消息" min-width="200" show-overflow-tooltip />
      </el-table>
      
      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="pagination.pageNum"
          v-model:page-size="pagination.pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          background
        />
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { Search, Refresh } from '@element-plus/icons-vue'
import { getLoginLogs } from '@/api/log'

// 查询条件
const queryParams = reactive({
  userId: '',
  startTime: '',
  endTime: ''
})

// 分页参数
const pagination = reactive({
  pageNum: 1,
  pageSize: 10,
})

// 列表数据
const logList = ref([])
const total = ref(0)
const loading = ref(false)

// 获取登录日志列表
const fetchLoginLogs = async () => {
  loading.value = true
  try {
    const params = {
      ...queryParams,
      ...pagination
    }
    // 移除空值参数
    Object.keys(params).forEach(key => {
      if (params[key] === '' || params[key] === null || params[key] === undefined) {
        delete params[key]
      }
    })
    const res = await getLoginLogs(params)
    if (res.code === 200) {
      logList.value = res.data.records || res.data.list || []
      total.value = res.data.total || 0
    } else {
      ElMessage.error(res.msg || '获取登录日志失败')
    }
  } catch (error) {
    console.error('获取登录日志异常:', error)
    ElMessage.error('获取登录日志失败')
  } finally {
    loading.value = false
  }
}

// 搜索
const handleQuery = () => {
  pagination.pageNum = 1
  fetchLoginLogs()
}

// 重置
const resetQuery = () => {
  queryParams.userId = ''
  queryParams.startTime = ''
  queryParams.endTime = ''
  handleQuery()
}

// 分页大小改变
const handleSizeChange = (size) => {
  pagination.pageSize = size
  fetchLoginLogs()
}

// 当前页改变
const handleCurrentChange = (page) => {
  pagination.pageNum = page
  fetchLoginLogs()
}

// 页面加载时获取数据
onMounted(() => {
  fetchLoginLogs()
})
</script>

<style scoped>
/* 沿用全局样式和布局样式 */
</style> 